package com.price.sample.ui

import android.os.Bundle
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.price.common.base.BaseFragment
import com.zp.common.R
import com.price.sample.ui.placeholder.PlaceholderContent

/**
 * A fragment representing a list of Items.
 */
class ItemFragment : BaseFragment() {

    private var columnCount = 1


    override fun getViewId(): Int = R.layout.fragment_item_list

    override fun initView(view: View, savedInstanceState: Bundle?) {
        arguments?.let {
            columnCount = it.getInt(ARG_COLUMN_COUNT)
        }
        if (view is RecyclerView) {
            with(view) {
                layoutManager = when {
                    columnCount <= 1 -> LinearLayoutManager(context)
                    else -> GridLayoutManager(context, columnCount)
                }
                adapter = MyItemRecyclerViewAdapter(PlaceholderContent.ITEMS)
            }
        }
    }

    override fun initData(savedInstanceState: Bundle?) {

    }

    companion object {

        // TODO: Customize parameter argument names
        const val ARG_COLUMN_COUNT = "column-count"

        // TODO: Customize parameter initialization
        @JvmStatic
        fun newInstance(columnCount: Int) =
            ItemFragment().apply {
                arguments = Bundle().apply {
                    putInt(ARG_COLUMN_COUNT, columnCount)
                }
            }
    }
}